﻿
-- ================================================
-- Remarks: 
-- ================================================
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

IF EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'Dasideen_Sys_SessionLog_EndSessionTimeUpdate')
	BEGIN
		DROP  Procedure  [Dasideen_Sys_SessionLog_EndSessionTimeUpdate]
	END

GO

CREATE PROCEDURE Dasideen_Sys_SessionLog_EndSessionTimeUpdate 
-- =============================================
-- Author:		<Sagar s.k>
-- Create date: <12/05/2015>
-- Description:	<Dasideen_Sys_SessionLog_EndSessionTimeUpdate>
-- =============================================
-- Add the parameters for the stored procedure here
	( 
	            @SessionLogPrimaryID int,
				@UserSessionID varchar(500),
				@UserIPAddress varchar(50) = NULL,
				@UserSessionStartTime datetime = NULL,
				@UserSessionEndTime datetime = NULL,
				@UserBrowserName nvarchar(150) = NULL,
				@UserKey int = NULL,

				@Sys_ModifiedByID varchar(50) = NULL,
				@Sys_DeleteType int = 0,
				@Sys_IsDeleted bit = false,
				@Sys_OrganizationID int = NULL,
				@Sys_InitialPrimaryKeys varchar(max)= NULL,
				@Sys_WorkstationName varchar(50)= NULL,
				@ConcurrencyTimeStamp datetime = NULL,

				@Sys_DateOfModification datetime OUTPUT
	)
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

	-- ConcurrencyTimeStamp is mandatory and it needs to check if any other user has changed the reord or not	
	IF @ConcurrencyTimeStamp = NULL
	BEGIN
		RAISERROR('Missing @ConcurrencyTimeStamp', 11,1);
	END
	DECLARE @CurrentModificationDate datetime;
	SET @CurrentModificationDate = (SELECT [Sys_DateOfModification] FROM [Sys_SessionLog] WHERE [SessionLogPrimaryID] = @SessionLogPrimaryID);
	If @ConcurrencyTimeStamp != @CurrentModificationDate
	BEGIN
		RAISERROR('Record is Changed by another process', 11,1);
	END
	
	IF @Sys_WorkstationName = 'Default'	
	BEGIN
		SET @Sys_WorkstationName = (SELECT @@SERVERNAME);
	END
	DECLARE @CurrentDataTime datetime;
	SET @CurrentDataTime = GETDATE();

    UPDATE	[Sys_SessionLog]
	SET		
			
			[UserSessionEndTime] = @CurrentDataTime,
			[UserBrowserName] = @UserBrowserName,
			[UserKey] = @UserKey,

			[Sys_DateOfModification] = @CurrentDataTime,
			[Sys_ModifiedByID] = @Sys_ModifiedByID
			
	WHERE	
	(
		[SessionLogPrimaryID] = @SessionLogPrimaryID
	);
	SET @Sys_DateOfModification = @CurrentDataTime;
END
GO
